package com.hrzyit.whistle.cases.ui.activity

import android.os.Bundle
import android.support.v4.app.Fragment
import com.hrzyit.base.ui.activity.BaseActivity
import com.hrzyit.whistle.cases.R
import com.hrzyit.whistle.cases.common.CaseConstant
import com.hrzyit.whistle.cases.ui.fragment.BaiDuMapFragment
import com.hrzyit.whistle.cases.ui.fragment.RecordAudioFragment
import java.util.*

/**
 * 案件媒体组件容器, 包括地图定位组件 Fragment 和 音频录制组件 Fragment
 */
class RegisterMediaActivity : BaseActivity() {

    // Fragment 栈
    private var mStack: Stack<Fragment> = Stack()

    // 定位地图 Fragment
    private val baiDuMapFragment: BaiDuMapFragment by lazy {
        BaiDuMapFragment()
    }
    // 录音 Fragment
    private val recordAudioFragment: RecordAudioFragment by lazy {
        RecordAudioFragment()
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_register_media)

        val fragmentKey = intent.getIntExtra("fragment_key", 0)

        initFragment()
        changeFragmentByTag(fragmentKey.toString())
    }

    /**
     * 初始化 Fragment 栈管理
     */
    private fun initFragment() {
        val manager = supportFragmentManager.beginTransaction()
        // 地图
        manager.add(
            R.id.mRegisterMedialContainer, baiDuMapFragment, CaseConstant.RESULT_CODE_MAP.toString()
        )
        // 录音
        manager.add(
            R.id.mRegisterMedialContainer, recordAudioFragment, CaseConstant.RESULT_CODE_RECORD_AUDIO.toString()
        )
        // 拍照？
        manager.commit()

        mStack.add(baiDuMapFragment)
        mStack.add(recordAudioFragment)
    }

    /**
     * 通过 Tag 切换 Fragment
     */
    private fun changeFragmentByTag(tag: String) {
        val manager = supportFragmentManager.beginTransaction()
        mStack.forEach {
            when (it.tag) {
                tag -> {
                    manager.show(it)
                }
                else -> {
                    manager.hide(it)
                }
            }
        }
        manager.commit()
    }
}